package com.qcby.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qcby.common.exception.BusinessException;
import com.qcby.entity.TProduct;
import com.qcby.form.TProductForm;
import com.qcby.form.TProductListForm;
import com.qcby.mapper.TProductMapper;
import com.qcby.service.TProductService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qcby.vo.PageVO;
import com.qcby.vo.TProductVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 商品服务实现类
 * </p>
 *
 * @author admin
 * @since 2025-02-04
 */
@Service
public class TProductServiceImpl extends ServiceImpl<TProductMapper, TProduct> implements TProductService {
    @Autowired
    private TProductMapper productMapper;

    @Override
    public PageVO productList(TProductListForm productListForm) {
        Page<TProduct> page = new Page<>(productListForm.getPage(), productListForm.getLimit());
        QueryWrapper<TProduct> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(StringUtils.isNotBlank(productListForm.getProductName()), "product_name", productListForm.getProductName())
                .like(StringUtils.isNotBlank(productListForm.getBrandName()), "brand_name", productListForm.getBrandName())
                .eq(productListForm.getCategoryId() != null, "category_id", productListForm.getCategoryId())
                .eq(productListForm.getStatus() != null, "status", productListForm.getStatus())
                .eq("is_deleted", 0)
                .orderByDesc("version");
                
        Page<TProduct> resultPage = this.productMapper.selectPage(page, queryWrapper);
        
        PageVO pageVO = new PageVO();
        List<TProductVO> list = new ArrayList<>();
        for (TProduct record : resultPage.getRecords()) {
            TProductVO productVO = new TProductVO();
            BeanUtils.copyProperties(record, productVO);
            list.add(productVO);
        }
        pageVO.setList(list);
        pageVO.setTotalCount(resultPage.getTotal());
        pageVO.setPageSize(resultPage.getSize());
        pageVO.setCurrPage(resultPage.getCurrent());
        pageVO.setTotalPage(resultPage.getPages());
        return pageVO;
    }

    @Override
    @Transactional
    public void addProduct(TProductForm form) {
        TProduct product = new TProduct();
        BeanUtils.copyProperties(form, product);
        product.setVersion(LocalDateTime.now());
        product.setIsDeleted(0);
        productMapper.insert(product);
    }

    @Override
    @Transactional
    public void updateProduct(Integer id, TProductForm form) {
        TProduct product = productMapper.selectById(id);
        if (product == null || product.getIsDeleted() == 1) {
            throw new BusinessException("商品不存在");
        }
        
        BeanUtils.copyProperties(form, product);
        product.setVersion(LocalDateTime.now());
        productMapper.updateById(product);
    }

    @Override
    public void deleteProduct(Integer id) {
        TProduct product = productMapper.selectById(id);
        if (product == null || product.getIsDeleted() == 1) {
            throw new BusinessException("商品不存在");
        }
        
        product.setIsDeleted(1);
        product.setVersion(LocalDateTime.now());
        productMapper.updateById(product);
    }

    @Override
    public TProductVO getProductDetail(Integer id) {
        TProduct product = productMapper.selectById(id);
        if (product == null || product.getIsDeleted() == 1) {
            throw new BusinessException("商品不存在");
        }
        
        TProductVO productVO = new TProductVO();
        BeanUtils.copyProperties(product, productVO);
        return productVO;
    }

    @Override
    public void onShelf(Integer id) {
        updateProductStatus(id, 1); // 1-上架
    }

    @Override
    public void offShelf(Integer id) {
        updateProductStatus(id, 0); // 0-下架
    }

    /**
     * 更新商品状态
     */
    private void updateProductStatus(Integer id, Integer status) {
        TProduct product = productMapper.selectById(id);
        if (product == null || product.getIsDeleted() == 1) {
            throw new BusinessException("商品不存在");
        }

        product.setStatus(status);
        product.setVersion(LocalDateTime.now());
        productMapper.updateById(product);
    }
}
